/*
 * Filename    : State_Gameplay.java
 * Projectname : RamaShinta
 * Date        : Nov 25, 2012 , 10:33:50 AM 
 * 
 */

package com.amikomgamedev.ramashinta.state;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.modifier.ScaleAtModifier;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.input.touch.TouchEvent;

import com.amikomgamedev.ramashinta.ButtonShootCooldown;
import com.amikomgamedev.ramashinta.ServerData;
import com.amikomgamedev.ramashinta.Utils;
import com.amikomgamedev.ramashinta.entity.Entity_Platform;
import com.amikomgamedev.ramashinta.entity.Entity_Rahwana;
import com.amikomgamedev.ramashinta.entity.Entity_Rama;
import com.amikomgamedev.ramashinta.entity.Entity_Shinta;
import com.amikomgamedev.ramashinta.graphic.Graphic_Anim;
import com.amikomgamedev.ramashinta.text.Text_Manager;

import android.view.KeyEvent;

public class State_Game_Play extends State_Game_Pause implements Graphic_Anim
{
	private final int		BTN_UP				= 2;
	private final int		BTN_DOWN			= 3;
	private final int		BTN_UP_PRESS		= 0;
	private final int		BTN_DOWN_PRESS		= 1;
	private final int		BTN_PANAH_IDLE		= 0;
	private final int		BTN_PANAH_PRESSED	= 1;
	private final int		IDLE				= 2;
	private final int		ATTACK				= 3;

	private Entity_Rama		mc_Rama;
	private Entity_Rahwana	mc_Rahwana;
	private Entity_Shinta	mc_Shinta;
	private Entity_Platform Platform;
	public static boolean			colItemChoco		= false;
	private float			stateWin			= -370;
	
	public static boolean timerText = true;
	public static boolean timerCelRama = true;
	public static boolean timerCelRahwana = true;
	
	private static boolean isShoot = true;


	protected void create_State_Game_Play()
	{
		graphic.attachGamePLayEntity();
		mc_Rama = new Entity_Rama(graphic.spr_Mc_Rama, graphic.spr_Celebrasi_Rama, this, graphic, sound);
		mc_Rahwana = new Entity_Rahwana(activity, graphic.spr_Mc_Rahwana, graphic.spr_Celebrasi_Rahwana, mc_Rama, graphic, sound, isShoot);
		mc_Shinta = new Entity_Shinta(activity, graphic.spr_Mc_Shinta, graphic);
		graphic.attachGamePlay();
//		Platform = new Entity_Platform(RES_WIDTH/2, RES_HEIGHT/2, graphic, 4);
		sound.playGameplayBGM();
		text.createTextLevel();
		for(int i=0;i<text.Level.length;i++){
			if(ServerData.getSelectedLevel()==i){
				Utils.TRACE(""+text.Level[i]);
				graphic.spr_Gameplay_bg.attachChild(text.Level[i]);
				text.Level[i].setPosition(0, RES_HEIGHT/2);
				text.Level[i].registerEntityModifier(new ScaleAtModifier(2, 0, 3, -Utils.getRatioW(150),0 ));
			}
		}
				if(timerText)
				{
					
				activity.getEngine().registerUpdateHandler(new TimerHandler(2.5f, new ITimerCallback() {
					
					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						for(int j=0;j<text.Level.length;j++){
							if(ServerData.getSelectedLevel()==j){
//						text.Level[j].registerEntityModifier(new ScaleAtModifier(2, 3, 0, -Utils.getRatioW(150),0 ));
								text.Level[j].setPosition(RES_WIDTH-text.Level[j].getWidth()-Utils.getRatioW(20), RES_HEIGHT/2);
						text.Level[j].registerEntityModifier(new ScaleAtModifier(2, 3, 0, Utils.getRatioW(150),0 ));
						
							}}
					}
				}));
				}				

	}


	@Override
	public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if (State_Menu_Current == STATE_GAME_PLAY)
		{
			Utils.TRACE("KLIK BUTTON PANAH");
		}
		super.onClick(pButtonSprite, pTouchAreaLocalX, pTouchAreaLocalY);
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY)
	{
		if (State_Menu_Current == STATE_GAME_PLAY)
		{
			if (pTouchArea.equals(graphic.spr_Menu_Pause_Btn) && (pSceneTouchEvent.isActionDown()))
			{
				// pause area

				if (Utils.isCheat())
				{
					// mEngine.runOnUpdateThread(new Runnable()
					// {
					//
					// @Override
					// public void run()
					// {
					// // TODO Auto-generated method stub
					// sound.stopGameplayBGM();
					// graphic.detachGameplay();
					// switchState(STATE_GAME_WIN);
					// }
					// });

				} else
				{
					sound.stopGameplayBGM();
					switchState(STATE_GAME_PAUSE);
				}
			}

			if (pSceneTouchEvent.isActionDown() || pSceneTouchEvent.isActionMove())
			{
				if (pTouchArea.equals(graphic.spr_Gameplay_Button_Move[BTN_UP]))
				{
					graphic.spr_Gameplay_Button_Move[BTN_UP].setVisible(false);
					graphic.spr_Gameplay_Button_Move[BTN_UP_PRESS].setVisible(true);
					mc_Rama.switch_State(MC_UP);
					// mc_Rahwana.switch_State(MC_UP);
				} else if (pTouchArea.equals(graphic.spr_Gameplay_Button_Move[BTN_DOWN]))
				{
					mc_Rama.switch_State(MC_DOWN);
					graphic.spr_Gameplay_Button_Move[BTN_DOWN].setVisible(false);
					graphic.spr_Gameplay_Button_Move[BTN_DOWN_PRESS].setVisible(true);
					// mc_Rahwana.switch_State(MC_DOWN);
				} else if (pTouchArea.equals(graphic.spr_Gameplay_Btn_Panah[BTN_PANAH_IDLE]))
				{
					graphic.spr_Gameplay_Btn_Panah[BTN_PANAH_IDLE].setVisible(false);
					graphic.spr_Gameplay_Btn_Panah[BTN_PANAH_PRESSED].setVisible(true);
					// graphic.detachButtonPanah();
					// graphic.detachButtonPanahPressed();
					if (colItemChoco)
					{
						if (!ButtonShootCooldown.getInstance().superSpeed())
							return false;

						synchronized (this)
						{
							Utils.TRACE("speedUp");
							mc_Rama.switch_State(MC_ATTACK);
							sound.playSoundPanah();
							// mc_Rahwana.switch_State(MC_ATTACK);
							mc_Rama.shoot();
							// mc_Rahwana.shoot();
						}
						return true;

					} else
					{

						if (!ButtonShootCooldown.getInstance().checkValid())
							return false;

						synchronized (this)
						{
							Utils.TRACE("attack");
							mc_Rama.switch_State(MC_ATTACK);
							sound.playSoundPanah();
							// mc_Rahwana.switch_State(MC_ATTACK);
							mc_Rama.shoot();
							// mc_Rahwana.shoot();
						}
						return true;
					}

				}
			} else if (pSceneTouchEvent.isActionUp())
			{
				mc_Rama.switch_State(MC_IDLE);
				graphic.spr_Gameplay_Btn_Panah[BTN_PANAH_IDLE].setVisible(true);
				graphic.spr_Gameplay_Button_Move[BTN_UP].setVisible(true);
				graphic.spr_Gameplay_Button_Move[BTN_DOWN].setVisible(true);
				// mc_Rahwana.switch_State(MC_IDLE);
				// if
				// (pTouchArea.equals(graphic.spr_Gameplay_Btn_Panah[BTN_PANAH_PRESSED]))
				// {
				// graphic.spr_Gameplay_Btn_Panah[BTN_PANAH_PRESSED].detachSelf();
				// // mc_Rahwana.switch_State(MC_UP);
				// }

			}

		}
		return super.onAreaTouched(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
	}

	@Override
	public void onUpdate(float pSecondsElapsed)
	{
		// float cekTimer= pSecondsElapsed;
		// Utils.TRACE("time "+String.valueOf(cekTimer));
		if (State_Menu_Current == STATE_GAME_PLAY)
		{

			// long max = Runtime.getRuntime().maxMemory();
			// long total = Runtime.getRuntime().totalMemory();
			// long free = Runtime.getRuntime().freeMemory();
			//
			// Log.d("Memorys", "Max = " + max);
			// Log.d("Memorys", "Total = " + total);
			// Log.d("Memorys", "Free = " + free);

			mc_Rama.onUpdate(pSecondsElapsed, mc_Shinta, mc_Rama);
			mc_Rahwana.onUpdate(pSecondsElapsed, mc_Shinta, pSecondsElapsed, mc_Rama);
			mc_Shinta.onUpdate(pSecondsElapsed);
			switch (CekKemenangan())
			{
				case BELOM_ADA_YANG_MENANG:

					break;

				case RAMA_MENANG:
					// System.out.print("Rama menang");

					mEngine.runOnUpdateThread(new Runnable()
					{

						@Override
						public void run()
						{
							if(timerCelRama)
						{
							graphic.spr_Celebrasi_Rama.setPosition(graphic.spr_Mc_Rama.getX(), graphic.spr_Mc_Rama.getY());
							graphic.spr_Celebrasi_Rama.setVisible(true);
							graphic.spr_Mc_Rama.setVisible(false);
							mc_Rama.switch_State(MC_CELEBRASI2);
							activity.getEngine().registerUpdateHandler(new TimerHandler(2.5f, new ITimerCallback() {
								
								@Override
								public void onTimePassed(TimerHandler pTimerHandler) {
									
									if(State_Menu_Current==STATE_GAME_PLAY){
										sound.stopGameplayBGM();
										graphic.spr_Mc_Rama.setVisible(true);
										graphic.spr_Celebrasi_Rama.setVisible(false);
										graphic.detachGameplay();
										text.detachLevel();
										switchState(STATE_GAME_WIN);
										Utils.TRACE("attach win");
										
										}
								}
							}));
							}
							// TODO Auto-generated method stub
							
						}
					});

					break;

				case RAHWANA_MENANG:
					System.out.print("Rahwana menang");
					mEngine.runOnUpdateThread(new Runnable()
					{

						@Override
						public void run()
						{
							// TODO Auto-generated method stub
							if(timerCelRahwana)
							{
								isShoot = false;
								graphic.spr_Celebrasi_Rahwana.setPosition(graphic.spr_Mc_Rahwana.getX(), graphic.spr_Mc_Rahwana.getY());
								graphic.spr_Celebrasi_Rahwana.setVisible(true);
								graphic.spr_Mc_Rahwana.setVisible(false);
								mc_Rahwana.switch_State(MC_CELEBRASI);
								activity.getEngine().registerUpdateHandler(new TimerHandler(2.5f, new ITimerCallback() {
									
									@Override
									public void onTimePassed(TimerHandler pTimerHandler) {
										if(State_Menu_Current==STATE_GAME_PLAY){
													
											isShoot = true;
											sound.stopGameplayBGM();
											graphic.spr_Mc_Rahwana.setVisible(true);
											graphic.spr_Celebrasi_Rahwana.setVisible(false);
											graphic.detachGameplay();
											text.detachLevel();
											switchState(STATE_GAME_OVER);
											Utils.TRACE("attach lose");
											
											}
									}
								}));
								}
						}
					});
					break;

			}

			CekKemenangan();
		}
		super.onUpdate(pSecondsElapsed);
	}

	final static int	RAMA_MENANG				= 1;
	final static int	RAHWANA_MENANG			= 2;
	final static int	BELOM_ADA_YANG_MENANG	= 0;

	int CekKemenangan()
	{

		if (mc_Rama.barRama.spr[1].getY() <= stateWin)
		{
			return RAMA_MENANG;

		} else if (mc_Rahwana.barRahwana.spr[1].getY() <= stateWin)
		{
			return RAHWANA_MENANG;
		} else
		{
			return BELOM_ADA_YANG_MENANG;
		}

	}

	@Override
	public synchronized void onResumeGame()
	{
		// TODO Auto-generated method stub
		if (State_Menu_Current == STATE_GAME_PLAY)
		{
			sound.playGameplayBGM();
		}
		super.onResumeGame();
	}

	@Override
	public synchronized void onPauseGame()
	{
		// TODO Auto-generated method stub
		if (State_Menu_Current == STATE_GAME_PLAY)
		{

			sound.stopGameplayBGM();
		}
		super.onPauseGame();
	}

}
